package cap.action;

import cap.bean.User;
import cap.dao.AdminDao;
import cap.dao.UserDao;
import cap.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String email=request.getParameter("email");
        UserDao userDao=new UserDaoImpl();
        int a=userDao.register(email,username,password);
        if(a>0){
            request.getSession().setAttribute("successMsg","注册成功");
        }else {
            request.setAttribute("errorMsg", "注册失败，请重新填写用户信息");
        }
        response.sendRedirect("register.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
